from __future__ import annotations

import json
import time
from pathlib import Path
from typing import Any, Dict

from .models import GenerationOutput


class HistoryStore:
    def __init__(self, base_dir: str = "history") -> None:
        self.base = Path(base_dir)
        self.base.mkdir(parents=True, exist_ok=True)

    def _next_path(self) -> Path:
        ts = time.strftime("%Y%m%d_%H%M%S", time.localtime())
        return self.base / f"generation_{ts}.json"

    def save_json(self, data: Dict[str, Any]) -> str:
        path = self._next_path()
        with path.open("w", encoding="utf-8") as f:
            json.dump(data, f, ensure_ascii=False, indent=2)
        return str(path)